package com.dehong.controller;

import com.dehong.model.ContractC;
import com.dehong.model.ContractProductC;
import com.dehong.service.ContractCService;
import com.dehong.service.ContractProductCService;
import com.dehong.service.ExtCproductCService;
import com.dehong.vo.ContractCVo;
import com.dehong.vo.ExContractPCVo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Controller
public class ContractcController {
    @Autowired
    private ContractCService contractCService;
    @Autowired
    ContractProductCService contractProductCService;
    @Autowired
    ExtCproductCService extCproductCService;
    @InitBinder
    public  void init(WebDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }
    @RequestMapping("selectExportc")
    public String selectExportc(Model model){

        List<ContractCVo> selectcontractc = contractCService.selectcontractc();
        for (ContractCVo contractC : selectcontractc) {
            List<ContractProductC> contractProductCS = contractProductCService.selectBycid(contractC.getContractId());
            contractC.setCargo_number(contractProductCS.size());
            if (contractProductCS.size()==0){
                contractC.setAccessory_number(0);
                contractC.setTotal(0);
            }
            for (ContractProductC contractProductC : contractProductCS) {
                List<ExContractPCVo> selectextpc = extCproductCService.selectextpc(contractProductC.getContractProductId());
                contractC.setAccessory_number(selectextpc.size());
                int cargo_total = contractProductC.getPrice().intValue() * contractProductC.getCnumber().intValue();
                int sum=0;
                for (ExContractPCVo exContractPCVo : selectextpc) {
                    sum+=exContractPCVo.getCnumber().intValue()*exContractPCVo.getPrice().intValue();
                }

                contractC.setTotal(cargo_total+sum);
            }
        }
        model.addAttribute("dataList",selectcontractc);
        return "/pages/basicinfo/export/jContractList";
    }
    @RequestMapping("selectContractc")
    public String selectContractc(Model model){

        List<ContractCVo> selectcontractc = contractCService.selectcontractc();
        for (ContractCVo contractC : selectcontractc) {
            List<ContractProductC> contractProductCS = contractProductCService.selectBycid(contractC.getContractId());
                 contractC.setCargo_number(contractProductCS.size());
                 if (contractProductCS.size()==0){
                     contractC.setAccessory_number(0);
                     contractC.setTotal(0);
                 }
            for (ContractProductC contractProductC : contractProductCS) {
                List<ExContractPCVo> selectextpc = extCproductCService.selectextpc(contractProductC.getContractProductId());
                contractC.setAccessory_number(selectextpc.size());
                int cargo_total = contractProductC.getPrice().intValue() * contractProductC.getCnumber().intValue();
                 int sum=0;
                for (ExContractPCVo exContractPCVo : selectextpc) {
                       sum+=exContractPCVo.getCnumber().intValue()*exContractPCVo.getPrice().intValue();
                }

                contractC.setTotal(cargo_total+sum);
            }
        }
        model.addAttribute("dataList",selectcontractc);
        return "/pages/basicinfo/contractc/ContractList";
    }
    @RequestMapping("goinsertContractc")
    public String goinsertContractc(Model model){
        return "/pages/basicinfo/contractc/ContractCreate";
    }
    @RequestMapping("insertContractc")
    public String insertContractc(ContractC contractC){
        contractC.setContractId(UUID.randomUUID().toString());
        contractC.setState(BigDecimal.valueOf(0));
        contractC.setOutState(BigDecimal.valueOf(0));
        int i = contractCService.insertSelective(contractC);
        if (i>0){
            return "redirect:/selectContractc.action";
        }else {
          return "error";
        }

    }
    @RequestMapping("goupdateContractc")
    public String goupdateContractc(Model model,String contractId ){
        ContractC contractC1 = contractCService.selectByPrimaryKey(contractId);
        model.addAttribute("list",contractC1);
        return "/pages/basicinfo/contractc/ContractUpdate";
    }
    @RequestMapping("updateContractc")
    public String updateContractc(ContractC contractC){
        int i = contractCService.updateByPrimaryKeySelective(contractC);
        if (i>0){
            return "redirect:/selectContractc.action";
        }else {
            return "error";
        }
    }
    @RequestMapping("deleteContract")
    public  String  deleteContract(String  contractId []){
        int j =0;
        for (String s : contractId) {
            int i = contractCService.deleteByPrimaryKey(s);
            if (i>0){
                j=j+i;
            }
        }
        if (j==contractId.length){
            return "redirect:/selectContractc.action";
        }else {
            return  "error";
        }

    }

    @RequestMapping("uploadAndremoveContract")
    public String uploadAndremoveContract(String []  contractId,int choose) {
        int j = 0;
        if (choose==1) {
            for (String s : contractId) {
                j++;
                ContractC contractC = contractCService.selectByPrimaryKey(s);
                contractC.setState(BigDecimal.valueOf(0));
                int i = contractCService.updateByPrimaryKeySelective(contractC);
                if (i > 0) {
                    if (j == contractId.length) {
                        return "redirect:/selectContractc.action";
                    }
                } else {
                    return "error";
                }
            }
            return "redirect:lectContractc.action";
        } else {
            for (String s : contractId) {
                j++;
                ContractC contractC = contractCService.selectByPrimaryKey(s);
                contractC.setState(BigDecimal.valueOf(1));
                int i = contractCService.updateByPrimaryKeySelective(contractC);
                if (i > 0) {
                    if (j == contractId.length) {
                        return "redirect:/selectContractc.action";
                    }
                } else {
                    return "error";
                }

            }
            return "redirect:lectContractc.action";
        }
    }

    //打印
    @RequestMapping("excel")
    public  void excel(HttpServletResponse response) throws IOException {
        List<ContractCVo> selectcontractc = contractCService.selectcontractc();
        for (ContractCVo contractC : selectcontractc) {
            List<ContractProductC> contractProductCS = contractProductCService.selectBycid(contractC.getContractId());
            contractC.setCargo_number(contractProductCS.size());
            if (contractProductCS.size()==0){
                contractC.setAccessory_number(0);
                contractC.setTotal(0);
            }
            for (ContractProductC contractProductC : contractProductCS) {
                List<ExContractPCVo> selectextpc = extCproductCService.selectextpc(contractProductC.getContractProductId());
                contractC.setAccessory_number(selectextpc.size());
                int cargo_total = contractProductC.getPrice().intValue() * contractProductC.getCnumber().intValue();
                int sum=0;
                for (ExContractPCVo exContractPCVo : selectextpc) {
                    sum+=exContractPCVo.getCnumber().intValue()*exContractPCVo.getPrice().intValue();
                }
                contractC.setTotal(cargo_total+sum);
            }
        }
         //创建excel文件

        HSSFWorkbook wb=new HSSFWorkbook();
        //创建sheet也

        HSSFSheet sheet=wb.createSheet("客户信息表");
        int rowNo = 0;							//行号
        int colNo = 1;
        Row nRow = null;
        Cell nCell = null;
        //合并的单元格样式
        HSSFCellStyle Style = wb.createCellStyle();
         Style.setAlignment(HorizontalAlignment.CENTER);
        HSSFRow title=sheet.createRow(0);
        nRow = sheet.createRow(rowNo++);
        nRow.setHeightInPoints(36);

        nCell = nRow.createCell(1);


        nCell.setCellValue("2020-1-1".replaceFirst("-0", "-").replaceFirst("-", "年") + "月份出货表");		//yyyy-MM


        //创建标题行
        HSSFRow hr=sheet.createRow(1);
        hr.createCell(0).setCellValue("序号");
        hr.createCell(1).setCellValue("客户名称");
        hr.createCell(2).setCellValue("货物数/附件数");
        hr.createCell(3).setCellValue("签单日期");
        hr.createCell(4).setCellValue("制单人");
        hr.createCell(5).setCellValue("审单人");
        hr.createCell(6).setCellValue("验货员");
        hr.createCell(7).setCellValue("船期");
        hr.createCell(8).setCellValue("交货期限");
        hr.createCell(9).setCellValue("打印版式");
        hr.createCell(10).setCellValue("总金额");
        hr.createCell(11).setCellValue("状态");
        hr.createCell(12).setCellValue("合同号");
        int excl_id=0;
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
        for (ContractCVo contractCVo : selectcontractc) {
            excl_id++;
            HSSFRow datarow=sheet.createRow(sheet.getLastRowNum()+1);
            datarow.createCell(0).setCellValue(excl_id);
            datarow.createCell(1).setCellValue(contractCVo.getCustomName());
            datarow.createCell(2).setCellValue(contractCVo.getCargo_number()+"/"+contractCVo.getAccessory_number());
            datarow.createCell(3).setCellValue(sim.format(contractCVo.getSigningDate()));
            datarow.createCell(4).setCellValue(contractCVo.getInputBy());
            datarow.createCell(5).setCellValue(contractCVo.getCheckBy());
            datarow.createCell(6).setCellValue(contractCVo.getInspector());
            datarow.createCell(7).setCellValue(sim.format(contractCVo.getShipTime()));
            datarow.createCell(8).setCellValue(sim.format(contractCVo.getDeliveryPeriod()));
            datarow.createCell(9).setCellValue(contractCVo.getPrintStyle());
            datarow.createCell(10).setCellValue(contractCVo.getTotal());
            datarow.createCell(11).setCellValue(contractCVo.getState().intValue());
            datarow.createCell(12).setCellValue(contractCVo.getContractNo());
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("客户名单".getBytes(),"iso-8859-1") + ".xls");
            OutputStream ouputStream = response.getOutputStream();
            if (excl_id==selectcontractc.size()){
                wb.write(ouputStream);
                ouputStream.flush();
                ouputStream.close();
            }
        }
    }
}
